package com.uam.core.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uam.core.dao.elasticsearch.ElasticsearchDao;
import com.uam.core.entity.enums.ElasticsearchIndices;
import com.uam.core.entity.model.ElasticsearchQueryResult;
import com.uam.core.entity.model.ElasticsearchQueryResult.BaseStationFields;
import com.uam.core.entity.model.ElasticsearchQueryResult.CommonFields;

/**
 * 基站管理
 * 
 * @author lishf
 */
@Service
public class BaseStationService {

	@Autowired
	private ElasticsearchDao elasticsearchDao = null;

	public boolean update(String id, Map<String, Object> data) {
		data.remove(CommonFields.POSITION.getName());
		Map<String, Object> finalData = new HashMap<String, Object>();
		Iterator<Entry<String, Object>> iterator = data.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, Object> entry = iterator.next();
			String key = entry.getKey().trim();
			if (!key.startsWith("polarization")) {
				if (CommonFields.NAME.getName().equalsIgnoreCase(key)) {
					finalData.put(key, (String) entry.getValue());
				} else if (BaseStationFields.FREQUENCY.getName().equalsIgnoreCase(key)) {
					finalData.put(key, ((Integer) entry.getValue()).toString());
				} else if (BaseStationFields.MINFREQ.getName().equalsIgnoreCase(key)
						|| BaseStationFields.MAXFREQ.getName().equalsIgnoreCase(key)) {
					finalData.put(key, Double.valueOf(((Integer) entry.getValue()).doubleValue()));
				} else if (CommonFields.STATUS.getName().equalsIgnoreCase(key)) {
					finalData.put(key, Integer.valueOf((String) entry.getValue()));
				} else {
					finalData.put(key, (Integer) entry.getValue());
				}
			}
		}
		return this.elasticsearchDao.update(ElasticsearchIndices.BASE_STATION_INDEX, id, finalData);
	}

	public Map<String, Object> detail(String id) {
		ElasticsearchQueryResult baseStationResult = this.elasticsearchDao
				.detail(ElasticsearchIndices.BASE_STATION_INDEX, id);
		if (ElasticsearchQueryResult.EMPTY != baseStationResult) {
			return baseStationResult.getData().get(0);
		}
		return null;
	}
}
